if Courier == nil then
    Courier = {}
    Courier.__index = Courier
end

function Courier:new( o )
    o = o or {}
    setmetatable( o, Courier )
    return o
end



function Courier:Init()

end

function Courier:Create(hHero)
    local slot1Item = hHero:GetItemInSlot(0)
    if slot1Item then
        hHero:TakeItem(slot1Item)
    end

    -- Create couriers and then cast them straight away
    local courier = hHero:AddItemByName('item_courier')
    if courier then
        hHero:CastAbilityImmediately(courier, hHero:GetPlayerID())
    end
    local courierUnit = PlayerResource:GetNthCourierForTeam(0, hHero:GetTeamNumber())
    if courierUnit~=nil then
        courierUnit:UpgradeToFlyingCourier()
        courierUnit:SetAbsOrigin(Vector(0,0,3500))
        courierUnit:SetBaseMaxHealth(100)
        courierUnit:SetMaxHealth(100)
        courierUnit:SetBaseHealthRegen(0)
        courierUnit:SetBaseMagicalResistanceValue(100)
        local ability
        for i=0,12 do
            ability= courierUnit:GetAbilityByIndex(i)
            if ability~=nil then
                courierUnit:RemoveAbility(ability:GetAbilityName())
                --courierUnit:RemoveModifierByName(ability:GetAbilityName())
            end
         end
        courierUnit:RemoveModifierByName("modifier_magic_immune")
        courierUnit:RemoveModifierByName("modifier_phased")
        courierUnit:SetOwner(hHero)
        courierUnit:SetTeam(hHero:GetTeamNumber())
        courierUnit:AddAbility(AbilitiesNames.ability_tp):SetLevel(1)
        ability=courierUnit:AddAbility(AbilitiesNames.ability_respawn_hero)
        ability:SetLevel(1)
        ability:SetHidden(true)
        courierUnit:AddNoDraw()
        courierUnit:AddNewModifier(courierUnit, nil, AbilitiesNames.modifier_dummy, {})
        courierUnit:AddNewModifier(courierUnit, nil, AbilitiesNames.modifier_courier, {})
        --courierUnit:SetTeam(DOTA_TEAM_NEUTRALS)
        courierUnit:SetControllableByPlayer(hHero:GetPlayerID(),true)

        --for _,value in pairs(courierUnit:FindAllModifiers()) do
        --   print(value:GetName())
        --end
        print("create courier success",hHero:GetTeamNumber())
    else
        error("create courier error")
    end
end

function Courier:RespawnHero(hero)
    local courier= PlayerResource:GetNthCourierForTeam(0, hero:GetTeamNumber())
    if courier~=nil then
        print("[RespawnHero]",courier:GetAbsOrigin())
        local tpAbility=courier:FindAbilityByName(AbilitiesNames.ability_tp)
        tpAbility.nextPos=hero:GetAbsOrigin()+ RandomVector(100)
        tpAbility:CastAbility()
        Timers:CreateTimer(10*FrameTime(),function ()
            if courier.isTp==false then
                Timers:CreateTimer(10*FrameTime(),function ()
                    if courier~=nil and courier.isTp==false and courier:IsAlive()==true then
                        courier:RemoveModifierByName(AbilitiesNames.modifier_dummy)
                        courier:CastAbilityOnPosition(hero:GetAbsOrigin(),courier:FindAbilityByName(AbilitiesNames.ability_respawn_hero),-1)
                    end
                end)
            else
                return 10*FrameTime()
            end
        end)
    else
        print("[RespawnHero] error null courier")
    end
end

function Courier:Hide(courier)
    courier:EmitSound(SoundsNames.tp_start)
    courier:AddNoDraw()
    courier:AddNewModifier(courier, nil, AbilitiesNames.modifier_dummy, {})
    local color=GameRules.G.teamColorList[courier:GetTeamNumber()]
    local pIndex=  ParticleManager:CreateParticle(ParticleNames.tp_start, PATTACH_ABSORIGIN, Hero:FastDummy(courier:GetAbsOrigin(),courier:GetTeam(),3))
    ParticleManager:SetParticleControl(pIndex, 2, Vector(color[1]/255,color[2]/255,color[3]/255))
    ParticleManager:ReleaseParticleIndex(pIndex)
    Timers:CreateTimer(40*FrameTime(),function ()
        courier:SetAbsOrigin(Vector(0,0,-2000))
    end)
end